package com.simtop.car.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.car.common.ServerResponse;
import com.simtop.car.mapper.AdminMapper;
import com.simtop.car.pojo.*;
import com.simtop.car.service.AdminService;
import com.simtop.car.utils.EmptyUtil;
import com.simtop.car.utils.PropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    public AdminMapper adminMapper;

    private static String netPath= PropertiesUtils.getNetPath();

    @Override
    public ServerResponse<String> addBrandType(Integer typeId, String brandName,String companyName) {
        BrandType brandType=adminMapper.checkBrandType(brandName,companyName);
        if(EmptyUtil.isNotEmpty(brandType)){
            return ServerResponse.createByErrorMsg("该汽车品牌已存在");
        }
        int flag=adminMapper.addBrandType(typeId,brandName,companyName);
        if(flag!=0){
            return ServerResponse.createBySuccessMsg("添加成功");
        }else{
            return ServerResponse.createByErrorMsg("添加失败");
        }
    }

    @Override
    public ServerResponse<String> deleteBrandType(Integer brandTypeId) {
        int delBrandType=adminMapper.deleteBrandType(brandTypeId);
        if(delBrandType!=0){
            return ServerResponse.createBySuccessMsg("成功删除汽车品牌");
        }else{
            return ServerResponse.createByErrorMsg("删除汽车品牌失败");
        }
    }

    @Override
    public ServerResponse<String> updateBrandType(BrandType brandType) {
//        BrandType updateBrandType=adminMapper.checkBrandType(null,brandType.getCompanyName());
//        if(EmptyUtil.isNotEmpty(updateBrandType)){
//            return ServerResponse.createByErrorMsg("该汽车品牌已经存在");
//        }
        int updateStatus=adminMapper.updateBrandType(brandType);
        if(updateStatus!=0){
            return ServerResponse.createBySuccess("修改成功");
        }else{
            return ServerResponse.createByErrorMsg("修改失败");
        }
    }


    @Override
    public void deleteUser() {
        adminMapper.deleteFormData();
        long ms=System.currentTimeMillis();
        Date date=new Date(ms-1000*60*60*24*1);
        adminMapper.deleteUser(date);
    }


    @Override
    public ServerResponse updateVideo(Video video) {
        int updateVideoStatus=adminMapper.updateVideo(video);
        if(updateVideoStatus!=0){
            return ServerResponse.createBySuccess("成功修改视频");
        }else{
            return ServerResponse.createByErrorMsg("修改视频失败");
        }
    }

    @Override
    public ServerResponse getIndexVideo() {
        List<Video> videoList=adminMapper.getIndexVideo();
        videoList.forEach(video -> {
            video.setVideoPath(netPath+video.getVideoPath());
        });
        return ServerResponse.createBySuccess(videoList);
    }


    @Override
    public ServerResponse getTitle(Integer titleId) {
       Title title=adminMapper.getTitle(titleId);
        return ServerResponse.createBySuccess(title);
    }

    @Override
    public ServerResponse updateTitle(Title title) {
        int updateTitleStatus=adminMapper.updateTitle(title);
        if(updateTitleStatus!=0){
            return ServerResponse.createBySuccess("成功修改标题");
        }else {
            return ServerResponse.createByErrorMsg("修改标题失败");
        }
    }


    @Override
    public ServerResponse deleteSourceData(Integer sourceId, String dataIds) {
        String[] dataIdsArray=dataIds.split(",");
        int[] dataIdArray=new int[dataIdsArray.length];
        for (int i = 0; i <dataIdsArray.length; i++) {
            dataIdArray[i]=Integer.parseInt(dataIdsArray[i]);
        }
        Integer deleteSourceDataStatus=0;
        if(sourceId==1){
            /**
             * 微博
             */
            deleteSourceDataStatus=adminMapper.deleteWeiBo(dataIdArray);
        }else if(sourceId==2){
            /**
             * 微信
             */
            deleteSourceDataStatus=adminMapper.deleteWeChat(dataIdArray);
        }else if(sourceId==3){
            /**
             * 新闻
             */
            deleteSourceDataStatus=adminMapper.deleteNews(dataIdArray);
        }else if(sourceId==4){
            /**
             * 招聘
             */
            deleteSourceDataStatus=adminMapper.deleteRecruitment(dataIdArray);
        }else if(sourceId==5){
            /**
             * 专利
             */
            deleteSourceDataStatus=adminMapper.deletePatent(dataIdArray);
        }
        else if(sourceId==6){
            /**
             *企业官网
             */
            deleteSourceDataStatus=adminMapper.deleteCompanyWebsite(dataIdArray);

        }
        else if(sourceId==7){
            /**
             * 社区论坛
             */
            deleteSourceDataStatus=adminMapper.deleteCommunityForum(dataIdArray);
        }
        else if(sourceId==8){
            /**
             * 企业年报
             */
            deleteSourceDataStatus=adminMapper.deleteCorporateAnnualReports(dataIdArray);
        }
        else if(sourceId==9){
            /**
             * 工商信息
             */
            deleteSourceDataStatus=adminMapper.deleteBusinessInformation(dataIdArray);
        }
        if(deleteSourceDataStatus!=0){
            return ServerResponse.createBySuccessMsg("成功删除");
        }else {
            return ServerResponse.createByErrorMsg("删除失败");
        }
    }


    @Override
    public ServerResponse addDiscuss(Grade grade) {
        Integer addDiscussStatus=adminMapper.addDiscuss(grade);
        if(addDiscussStatus!=0){
            return ServerResponse.createBySuccess("成功添加评论内容");
        }else{
            return ServerResponse.createByErrorMsg("添加评论失败");
        }
    }


    @Override
    public ServerResponse addManual(Manual manual) {
        Integer addManualStatus=adminMapper.addManual(manual);
        if(addManualStatus!=0){
            return ServerResponse.createBySuccess("成功添加");
        }else{
            return ServerResponse.createByErrorMsg("添加失败");
        }
    }

    @Override
    public ServerResponse getManuals(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Manual> manualList=adminMapper.getManuals();
        PageInfo<Manual> manualPageInfo=new PageInfo<>(manualList);
        return ServerResponse.createBySuccess(manualPageInfo);
    }


    @Override
    public ServerResponse getSingleManual(Integer manualId) {
        try {
            Manual manual=adminMapper.getSingleManual(manualId);
            return ServerResponse.createBySuccess(manual);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("查询失败");
        }

    }

    @Override
    public ServerResponse deleteManual(Integer manualId) {
        Integer deleteManualStatus=adminMapper.deleteManual(manualId);
        if(deleteManualStatus!=0){
            return ServerResponse.createBySuccess("成功删除");
        }else{
            return ServerResponse.createByErrorMsg("删除失败");
        }
    }


    @Override
    public ServerResponse updateManual(Manual manual) {
        Integer updateManualStatus=adminMapper.updateManual(manual);
        if(updateManualStatus!=0){
            return ServerResponse.createBySuccessMsg("修改成功");
        }else{
            return ServerResponse.createByErrorMsg("修改失败");
        }
    }
}
